<?php
//APP扩展功能插件
class filesController extends adminController{
    protected $layout = 'layout';
    public function __construct(){
        $session_name = session_name();
        if (isset($_POST[$session_name])) {
            session_id($_POST[$session_name]);
            session_start();
        }
    }
    //附件管理
    public function index(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $result =  model('files')->DeleteFile($fid);
            if($result){
                echo 1;
            }else{
                echo '删除失败!';
            } 
        }else{
            $listRows = 15; //每页条数
            $url = url('files/index',array('page'=>'{page}'));
            $allcount = model('files')->count();
            $limit = $this->pageLimit($url,$listRows);
            $this->page= $this->pageShow($allcount);
            $this->info = model('files')->select('','id,path,name,uptime','id desc',$limit);
            $this->display('files/index'); 
        }
    } 
    //读取系统配置
    public function configs(){
        if($this->isPost()){
            $id =  intval($_POST['id']);
            $value = $_POST['value'];
            switch ($id) {          
                case 1:
                    $rel = save_config(APP_NAME, array('CPCMS_UPFOLDER'=>$value));
                    break;
                case 2:
                    $rel = save_config(APP_NAME, array('CPCMS_UPSIZE'=>$value));
                    break;
                case 3:
                    $rel = save_config(APP_NAME, array('CPCMS_UPEXTS'=>$value));
                    break;
                default:
                    $rel = FALSE;
                    break;
            }
            echo "修改成功";
        }else{
            $this->upfolder= config('CPCMS_UPFOLDER');
            $this->upexts = config('CPCMS_UPEXTS');
            $this->upsize = config('CPCMS_UPSIZE');
            $this->display('files/configs');     
        }
    }
    //文件在线管理
    public function manage(){
        $this->frameid = $_GET['id'];
        $this->display('files/manage');    
    }
    //读取最近的12个上传
    public function lists(){
        $this->type = $_POST['type'];
        $this->info = model('files')->select($condition,'path,path,name,uptime','id desc','0,6');
        $this->frameid = $_GET['id'];
        $this->display('files/lists'); 
    }
    //上传组件
    public function upload(){
        if($this->isPost()){
            $result = model('files')->uploadFile($_FILES);
            if(is_array($result)){
                $rel = model('files')->saveFile($result[0]);
                $arr['error'] = 0;
                $arr['url'] = __ROOT__.'/'.$result[0]['savepath'].$result[0]['savename'];               
                echo json_encode($arr);
            }else{
                $arr['error'] = 1;
                $arr['message'] = $result;               
                echo json_encode($arr);
            }  
        }else{
            $this->display('files/uploaddemo'); 
        }
    }
    //缩略图
    public function crop_images(){
        //本功能COPY自DLCMS
        $this->imgurl = $_GET['imgurl'];
        $this->frameid = $_GET['id'];
        $this->display('files/crop_images'); 
    }
    //缩略图保存
    public function crop_images_save(){
        $imgurl = $_POST['imgurl'];
        $w = $_POST['w'];
        $h = $_POST['h'];
        $frameid = $_POST['frameid'];
        $rel = model('files')->imagesthumb($imgurl,$w,$h);
        if($rel){
            $this->jssuccess('生成缩略图成功,请重复点击一下“图片库”标签可显示。',url('files/manage'.array('id'=>$frameid)));
        }else{
            $this->jserror('生成缩略图失败');
        }
    }
}